Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(js): handling the web socket connection and events #5704

Merged
merged 20 commits into from
Jun 20, 2024

Conversation

LetItRock
Copy link
Contributor

What changed? Why was the change needed?

The changes:

  • lazily connect to the WebSocket
  • allow to listen to WebSocket events
  • build UMD module with Webpack

Screenshots

Expand for optional sections

Related enterprise PR

Special notes for your reviewer

@LetItRock LetItRock self-assigned this Jun 10, 2024
Copy link

linear bot commented Jun 10, 2024

Copy link

netlify bot commented Jun 10, 2024

Deploy Preview for novu-design failed. Why did it fail? →

Name Link
🔨 Latest commit 3329087
🔍 Latest deploy log https://app.netlify.com/sites/novu-design/deploys/66744e31100aed0008a4ace3

@@ -1,4 +1,4 @@
import {
import type {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use import type everywhere in the @novu/client so that @novu/shared won't be included in the bundle

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💭 thought (non-blocking): If we are just using the types, maybe we can move the @novu/shared under the devDependency as well.‏

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't because of the types generated for ESM, they are not "copied" during the build time.

packages/js/package.json Show resolved Hide resolved
Comment on lines +40 to +42
protected onSessionSuccess(_: Session): void {}

protected onSessionError(_: unknown): void {}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two functions will be overridden by the class that extends this one.

@@ -25,7 +25,7 @@ export class NovuEventEmitter {
}

off<Key extends EventNames>(eventName: Key, listener: EventHandler<Events[Key]>): void {
this.#mittEmitter.on(eventName, listener);
this.#mittEmitter.off(eventName, listener);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my fault 😅

Comment on lines +40 to +44
if (this.#socket.isSocketEvent(eventName)) {
this.#socket.initialize();
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When someone tries to listen to one of the WS events then initialize the WebSocket connection and add listeners on events.

Comment on lines 35 to 51
#notificationReceived = ({ message }: { message: TODO }) => {
this.#emitter.emit(NOTIFICATION_RECEIVED, {
result: new Notification(message),
});
};

#unseenCountChanged = ({ unseenCount }: { unseenCount: number }) => {
this.#emitter.emit(UNSEEN_COUNT_CHANGED, {
result: unseenCount,
});
};

#unreadCountChanged = ({ unreadCount }: { unreadCount: number }) => {
this.#emitter.emit(UNREAD_COUNT_CHANGED, {
result: unreadCount,
});
};
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handle different types of WebSocket events and just call our emitter.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What type of information do we get from WS currently, except unread_count? I'd like to suggest to have one updated event for all WS signals for easier scalability in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can check the three above functions and their interfaces, that's what is called and returned

Comment on lines 61 to 79
this.#socketIo = io(this.#socketUrl, {
reconnectionDelayMax: 10000,
transports: ['websocket'],
query: {
token: `${this.#token}`,
},
});

this.#socketIo.on('connect', () => {
this.#emitter.emit('socket.connect.success', { args, result: undefined });
});

this.#socketIo.on('connect_error', (error) => {
this.#emitter.emit('socket.connect.error', { args, error });
});

this.#socketIo?.on(WebSocketEvent.RECEIVED, this.#notificationReceived);
this.#socketIo?.on(WebSocketEvent.UNSEEN, this.#unseenCountChanged);
this.#socketIo?.on(WebSocketEvent.UNREAD, this.#unreadCountChanged);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Create the socket connection and listen on the events.

Comment on lines 89 to 97
if (this.#token) {
this.#initializeSocket().then((error) => console.error(error));

return;
}

this.callWithSession(async () => {
this.#initializeSocket().then((error) => console.error(error));
});
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When there is a session just initialize the socket, otherwise, initialize it when the session is created (lazily).

Comment on lines 42 to 61
// {
// ...baseConfig,
// entry: { novu: 'src/umd.ts' },
// format: ['iife'],
// minify: true,
// dts: false,
// outExtension: () => {
// return {
// js: '.min.js',
// };
// },
// esbuildPlugins: [
// compress({
// gzip: true,
// brotli: false,
// outputDir: '.',
// exclude: ['**/*.map'],
// }),
// ],
// },
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commented out this code temporarily until we investigate why Rollup includes in UMD build some additional code that results in bigger bundle size.

@LetItRock LetItRock force-pushed the com-30-js-sdk-preferences branch from 713d403 to 708b237 Compare June 11, 2024 08:43
@LetItRock LetItRock force-pushed the com-30-js-sdk-preferences branch from b57c777 to 9b0473d Compare June 11, 2024 10:28
LetItRock and others added 17 commits June 20, 2024 16:21
* feat: ui solid

* revert: novu/js package

* feat: implement solidjs

* refactor: update file name

* fix: umd build

* refactor: remove types from config

* refactor: update the require ext

* refactor: remove unused deps

* refactor: rename app to inbox

* feat: use novuoptions type

* deat: update tsconfig

* feat: remove rootelement on dispose

* refactor: rename ui to inboxui

* feat: add ui to umd

* feat: update tsup build and removed umd for ui

* refactor: imports and babel

* chore: remove comment
Copy link

netlify bot commented Jun 20, 2024

Deploy Preview for dev-web-novu ready!

Name Link
🔨 Latest commit 3329087
🔍 Latest deploy log https://app.netlify.com/sites/dev-web-novu/deploys/66744e315e61a0000714ef7c
😎 Deploy Preview https://deploy-preview-5704--dev-web-novu.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@LetItRock LetItRock merged commit 87c7330 into next Jun 20, 2024
28 checks passed
@LetItRock LetItRock deleted the com-53-js-sdk-ws branch June 20, 2024 16:08
github-actions bot added a commit that referenced this pull request Jun 21, 2024
* feat(js): js sdk feeds module (#5688)

* feat(js): improve the package json exports and tsup config
* feat(js): lazy session initialization and interface fixes
* feat(js): js sdk feeds module

* feat(js): js sdk preferences (#5701)

* feat(js): js sdk preferences

* feat(js): handling the web socket connection and events (#5704)

* feat(js): handling the web socket connection and events
* feat: ui solid

---------

Co-authored-by: Biswajeet Das <[email protected]>

* fix: caching for session initialize

* fix: worker

---------

Co-authored-by: Paweł Tymczuk <[email protected]>
Co-authored-by: Biswajeet Das <[email protected]>
Co-authored-by: Dima Grossman <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants